require "lfs"
require "unity"
local mpi = require "luaMPI"

-----------------------------------------------------------------------------
--初始化参数
local MPIPond={
	model = {};
	model_runned = {};
	model_unrunned = {};
	info_path = "pond_info.txt";
	model_runed_num = 0;
	console_mode = true;
};
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--创建需要计算的模型
function MPIPond:init()
	self.model = {};
	self.model_runned = {};
	self.model_unrunned = {};
	self.model_runed_num = 0;
end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--创建表和创建元表
function MPIPond:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	self:init();
	return o;
end
-----------------------------------------------------------------------------

function MPIPond:insert_model(m)	
	table.insert(self.model, m) 
end

-----------------------------------------------------------------------------
--创建需要计算的模型
function MPIPond:create_models()
end
-----------------------------------------------------------------------------


local function file_exists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end

function MPIPond:find_results()	
	self.model_runned = {}
	self.model_unrunned = {}
	for k,v in pairs (self.model) do
		if file_exists(v.name..".gap") then
			table.insert( self.model_runned, v )
		else
			table.insert( self.model_unrunned, v )
		end
	end
end

function MPIPond:clear_results()	
	self.model_runned = {}
	self.model_unrunned = {}
	for k,v in pairs (self.model) do
		if file_exists(v.name..".gap") then
			print( v.name..".gap" )
			os.remove (v.name..".gap")
		end
	end
end

-----------------------------------------------------------------------------
--write the calculation data of partical to file
function MPIPond:writeStartCalInfo2File(name)
	local file = io.open(self.info_path, "a")
	file:write(os.date('[%Y-%m-%d %H:%M:%S]').."  processor["..id_groups.."] calculate "..name.."start...\n")
	file:close()	
end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--write the calculation data of partical to file
function MPIPond:writeFinishCalInfo2File(name)
	local file = io.open(self.info_path, "a")
	file:write(os.date('[%Y-%m-%d %H:%M:%S]').."  processor["..id_groups.."] calculate "..name.."finished\n")
	file:close()	
end
-----------------------------------------------------------------------------


-----------------------------------------------------------------------------
--3D视图操作
function MPIPond:perspective_view ()
	if self.console_mode == false then
		local QGallop = require"QGMainWindow"
		if QGallop~=nil and QGallop.getMainWindow then
			local mw = QGallop.getMainWindow();
			if mw~=nil then
				local view = mw:get3DView();
				if view~=nil then
					view:showEntireScene();
					view:perspective();
				end
			end
		end
	end
end
-----------------------------------------------------------------------------


function MPIPond:pre_calculate()

end

function MPIPond:post_calculate()
end

function MPIPond:run_model_post()
end

function MPIPond:run()
	mpi.world.barrier();
	 for ical,model in pairs(self.model_unrunned) do
	    if id_groups==(ical-1)%num_groups then
		print ( "calcluate:  "..model.name )
		Geometry.message('Progress Bar', math.floor((self.model_runed_num) / (#self.model_unrunned) * 100));
		self:writeStartCalInfo2File(model.name);
		model:init();
		model:run();				
		table.insert( self.model_runned , model );
		self:run_model_post();		
		self:writeFinishCalInfo2File(model.name);		
		self.model_runed_num = self.model_runed_num + 1;	
		Geometry.message('Progress Bar', math.floor((self.model_runed_num) / (#self.model_unrunned) * 100));
	    end
	end	
end

function MPIPond:test()
	print('test',#self.model_unrunned)
	mpi.world.barrier();
	 for ical,model in pairs(self.model_unrunned) do
	    if id_groups==(ical-1)%num_groups then
		print ( "calcluate:  "..model.name )
		 model:init();
	    end
	end	
end


return MPIPond;
